package com.jt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.mapper.ItemDescMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemCat;
import com.jt.pojo.ItemDesc;
import com.jt.service.ItemService;
import com.jt.vo.EasyUITable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jt.mapper.ItemMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
	
	@Autowired
	private ItemMapper itemMapper;
	@Autowired
	private ItemDescMapper itemDescMapper;

	//分页查询商品信息
	@Override
	public EasyUITable findItemByPage(int page, int rows) {
//		long total = itemMapper.selectCount(null);
//		//手写分页
//		int startIndex=(page-1)*rows;
//		List<Item> itemList= itemMapper.findItemByPage(startIndex,rows);

		IPage mppage = new Page(page,rows);
		QueryWrapper<Item> wrapper = new QueryWrapper<>();
		wrapper.orderByDesc("updated");
		mppage=itemMapper.selectPage(mppage,wrapper);
		long total = mppage.getTotal();
		List<Item> itemList = mppage.getRecords();
		return new EasyUITable(total,itemList);
	}

	//新增商品
	@Override
	@Transactional
	public void saveItem(Item item, ItemDesc itemDesc) {
		itemMapper.insert(item);
		itemDesc.setItemId(item.getId());
		itemDescMapper.insert(itemDesc);
	}

	//修改商品信息
	@Override
	@Transactional
	public void updateItem(Item item,ItemDesc itemDesc) {
		itemMapper.updateById(item);
		itemDesc.setItemId(item.getId());
		itemDescMapper.updateById(itemDesc);

	}

	//根据商品id删除商品
	@Override
	public void deleteItem(Long[] ids) {
		//手写sql
//		itemMapper.deleteItemByIds(ids);
//		itemDescMapper.deleteItemDescByIds(ids);
		
		List<Long> idsList = Arrays.asList(ids);
		itemMapper.deleteBatchIds(idsList);
		itemDescMapper.deleteBatchIds(idsList);
	}

	//商品上架、 下架
	@Override
	@Transactional
	public void updateStatus(Integer status, Long[] ids) {
//		QueryWrapper<Item> wrapper = new QueryWrapper<>();
//		wrapper.in("id",ids);
//		Item item = new Item();
//		item.setStatus(status);
//		itemMapper.update(item,wrapper);

		//手写sql
		itemMapper.updateStatus(ids, status);

	}




}
